#include "StaticSequenceTable.h"

// 基本增删查改接口
// 顺序表初始化
void SeqList::SeqListInit()
{
    for (int i = 0; i < MAX_SIZE; i++)
    {
        m_arr[i] = 0;
    }
    m_size = 0;
}
// 顺序表查找
void SeqList::SeqListFind(SLDataType x)
{
    for(int i = 0; i < m_size; i++)
    {
        if(x == m_arr[i])
        {
            cout << "找到了，下标为" << i << endl;
            return;
        }
    }
    cout << "没找到！" << endl;
}
// 顺序表在pos位置插入x
void SeqList::SeqListInsert(size_t pos, SLDataType x)
{
    // 判断满了没有
    if(m_size >= MAX_SIZE)
    {
        cout << "满了，无法插入！" << endl;
        return;
    }

    // 判断pos位置的合法性
    if(pos < 1 || pos > m_size + 1)
    {
        cout << "插入位置不合法！" << endl;
        return;
    }
    
    for(int i = m_size; i > pos - 1; i--)
    {
        m_arr[i] = m_arr[i - 1];
    }
    m_arr[pos - 1] = x;
    m_size++;
    cout << "插入成功！" << endl;
}

// 顺序表删除pos位置的值
void SeqList::SeqListErase(size_t pos)
{
    cout << "要删除的元素位置为" << pos << endl;
    if(m_size == 0)
    {
        cout << "顺序表空了，无法删除" << endl;
        return;
    }
    if(pos < 1 || pos > m_size)
    {
        cout << "删除位置不合法！" << endl;
        return;
    }
    for(int i = pos - 1; i < m_size; i++)
    {
        m_arr[i] = m_arr[i + 1];
    }
    m_size--;
    cout << "删除成功！" << endl;
}

// 顺序表销毁
void SeqList::SeqListDestory()
{
    for(int i = 0; i < m_size; i++)
    {
        m_arr[i] = 0;
    }
    m_size = 0;
    cout << "顺序表销毁成功！" << endl;
}

// 顺序表打印
void SeqList::SeqListPrint()
{
    if(m_size == 0)
    {
        cout << "顺序表为空，没有数据打印！" << endl;
        return;
    }
    for(int i = 0; i < m_size; i++)
    {
        cout << m_arr[i] << " ";
    }
    cout << endl;
}